//
//  HotAskTableViewCell.m
//  SHHCPhone
//
//  Created by Sheng long on 14-9-21.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "HotAskTableViewCell.h"

#define CELL_HEAD_IMAGE_SIZE 27
#define CELL_HEAD_IMAGE_GAP  8

#define CELL_TEXT_FONT  [UIFont boldSystemFontOfSize:15]

#define CELL_ASK_HEAD_YGAP     5
#define CELL_ASK_MAX_WIDTH     (CURRENT_CONTENT_WIDTH - CELL_HEAD_IMAGE_GAP*2 - CELL_HEAD_IMAGE_SIZE - 30)

#define CELL_ANSWERBG_ASK_YGAP      2
#define CELL_ANSWERBG_BOTTONGAP     18

#define CELL_ANSWER_BG_XWIDTH      8
#define CELL_ANSWER_BG_YWIDTH      16
#define CELL_ANSWER_MAX_WIDTH     (CELL_ASK_MAX_WIDTH - CELL_ANSWER_BG_XWIDTH*2)

@interface HotAskTableViewCell()
{
    UIImageView *_headImage;
    UILabel *_askLabel;
    UIImageView *_answerBg;
    UILabel *_answerLabel;
    UIView *_lineView;
}
@end

@implementation HotAskTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        _headImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"默认头像.png"]];
        [_headImage sizeToFit];
        [_headImage setCenter:CGPointMake(CELL_ASK_HEAD_YGAP+_headImage.frame.size.width/2, CELL_ASK_HEAD_YGAP+_headImage.frame.size.height/2)];
        [self.contentView addSubview:_headImage];
        [_headImage.layer setMasksToBounds:YES];
        [_headImage.layer setCornerRadius:_headImage.frame.size.width/2];
        
        _askLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [_askLabel setFont:CELL_TEXT_FONT];
        [_askLabel setNumberOfLines:0];
        [_askLabel setBackgroundColor:[UIColor clearColor]];
        [_askLabel setTextColor:[UIColor blackColor]];
        [self.contentView addSubview:_askLabel];
        
        _answerBg = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"答题-答题背景框.png"]  stretchableImageWithLeftCapWidth:100 topCapHeight:20]];
        [self.contentView addSubview:_answerBg];
        
        _answerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [_answerLabel setFont:CELL_TEXT_FONT];
        [_answerLabel setBackgroundColor:[UIColor clearColor]];
        [_answerLabel setTextColor:GET_COLOR(@"#bebebe")];
        [_answerLabel setNumberOfLines:0];
        [self.contentView addSubview:_answerLabel];
        
        _lineView = [[UIView alloc] initWithFrame:CGRectZero];
        [_lineView setBackgroundColor:GET_COLOR(@"#f1f1f1")];
        [self.contentView addSubview:_lineView];
    }
    return self;
}

-(void)showContent:(NSMutableDictionary*)dataDic cellSize:(CGSize)cellSize
{
    NSDictionary *dic1 = [dataDic objectForKey:@"user"];
    [_headImage setImageWithURL:[NSURL URLWithString:[dic1 valueForKey:@"headimg"]] placeholderImage:[UIImage imageNamed:@"默认头像.png"]];
    
    NSString *ask = [NSString stringWithFormat:@"%@:%@",[dic1 valueForKey:@"nickname"],[dataDic valueForKey:@"content"]];
    CGSize size1 = [z_UI returnHeightFloat:ask frontSize:CELL_TEXT_FONT frontWidth:CELL_ASK_MAX_WIDTH];
    [_askLabel setText:ask];
    [_askLabel setFrame:CGRectMake(CGRectGetMaxX(_headImage.frame)+CELL_HEAD_IMAGE_GAP, _headImage.frame.origin.y+CELL_ASK_HEAD_YGAP, size1.width, size1.height)];
    
    NSDictionary *dic2 = [dataDic objectForKey:@"reply"];
    if (dic2 && dic2.count) {
        [_answerLabel setHidden:NO];
        [_answerBg setHidden:NO];
        
        NSString *answer = [NSString stringWithFormat:@"%@:%@",[[dic2 objectForKey:@"user"] valueForKey:@"nickname"],[dic2 valueForKey:@"content"]];
        [_answerLabel setText:answer];
        
        CGSize size2 = [z_UI returnHeightFloat:answer frontSize:CELL_TEXT_FONT frontWidth:CELL_ANSWER_MAX_WIDTH];
        
        [_answerBg setFrame:CGRectMake(_askLabel.frame.origin.x, CGRectGetMaxY(_askLabel.frame)+CELL_ANSWERBG_ASK_YGAP, CELL_ASK_MAX_WIDTH, size2.height+CELL_ANSWER_BG_YWIDTH*2)];
        [_answerLabel setFrame:CGRectMake(_answerBg.frame.origin.x+CELL_ANSWER_BG_XWIDTH, _answerBg.frame.origin.y+CELL_ANSWER_BG_YWIDTH, size2.width, size2.height)];
    }else{
        [_answerLabel setHidden:YES];
        [_answerBg setHidden:YES];
    }
    
    [_lineView setFrame:CGRectMake(0, cellSize.height-1, cellSize.width, 1)];
}

+(void)getCellHeight:(NSMutableDictionary*)dic
{
    CGFloat height = 0;
    
    NSString *ask = [NSString stringWithFormat:@"%@:%@",[[dic objectForKey:@"user"] valueForKey:@"nickname"],[dic valueForKey:@"content"]];
    CGSize size1 = [z_UI returnHeightFloat:ask frontSize:CELL_TEXT_FONT frontWidth:CELL_ASK_MAX_WIDTH];
    height = CELL_HEAD_IMAGE_GAP+CELL_ASK_HEAD_YGAP+size1.height;
    
    NSDictionary *dic1 = [dic objectForKey:@"reply"];
    if (dic1 && dic1.count) {
        NSString *answer = [NSString stringWithFormat:@"%@:%@",[[dic1 objectForKey:@"user"] valueForKey:@"nickname"],[dic1 valueForKey:@"content"]];
        CGSize size2 = [z_UI returnHeightFloat:answer frontSize:CELL_TEXT_FONT frontWidth:CELL_ANSWER_MAX_WIDTH];
        
        height += (CELL_ANSWERBG_ASK_YGAP+CELL_ANSWER_BG_YWIDTH*2+size2.height);
    }
    
    height+=20;
    [dic setObject:[NSNumber numberWithFloat:height] forKey:@"cellheight"];
}

@end
